package com.yuedou.jylt.facelib.engine;

import android.os.Handler;
import android.os.Message;

import com.yuedou.jylt.facelib.core.model.FaceLandmarkInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 动作识别
 */
public class CheckFaceEngine {
    private static CheckFaceEngine checkFaceEngine;
    public static CheckFaceEngine getInstance(){
        if(checkFaceEngine == null){
            synchronized (CheckFaceEngine.class){
                checkFaceEngine = new CheckFaceEngine();
            }
        }
        return checkFaceEngine;
    }


    /**
     *  没有进入人脸框
     */
    public void noFace(){
        Message message = new Message();
        message.what = Const.ACTION_TYPE_NO_FACE ;
        handler.sendMessage(message);
    }

    /**
     * 检测动作
     */
    private int checkTime = 5000;//检测时间
    private long startTime = 0 ;
    private List<Integer> eyesActions = new ArrayList<>();
//    private List<Integer> mouthActions = new ArrayList<>();
    private List<Integer> headActions = new ArrayList<>();
    private int zy1,zy2,zyyt1,zyyt2;//,zz1,zz2
    public void onCheckAction(List<FaceLandmarkInfo> faces){
        if(faces == null || faces.size() == 0){
            return;
        }
        if(startTime == 0){
            startTime = System.currentTimeMillis();
            eyesActions = new ArrayList<>();
//            mouthActions = new ArrayList<>();
            headActions = new ArrayList<>();
        }
        FaceLandmarkInfo landmarkInfo = faces.get(0);//取第一张脸的数据
        if(landmarkInfo.roll > 10 || landmarkInfo.roll < -10){
            //没有正对屏幕
            Message message = new Message();
            message.what = Const.ACTION_TYPE_NOT_ZHENGDUI;
            handler.sendMessage(message);
        }else{
            if(landmarkInfo.yaw > 3){
                headActions.add(1);
                zyyt1++;
            }else if(landmarkInfo.yaw < -3){
                headActions.add(0);
                zyyt2++;
            }

            if(landmarkInfo.pitch < -18){
                //抬头
                Message message = new Message();
                message.what = Const.ACTION_TYPE_RISE_HEAD ;
                handler.sendMessage(message);
            }else if(landmarkInfo.pitch > 18){
                //低头
                Message message = new Message();
                message.what = Const.ACTION_TYPE_BOW_HEAD;
                handler.sendMessage(message);
            }else {
                if(landmarkInfo.leftEyeClose > 0.85f || landmarkInfo.rightEyeClose > 0.85){
                    eyesActions.add(1);
                    zy1++;//
                }else{
                    eyesActions.add(0);
                    zy2++;
                }
//                if(landmarkInfo.mouseOpenBig > 0.85){
//                    mouthActions.add(1);
//                    zz1++;
//                }else{
//                    mouthActions.add(0);
//                    zz2++;
//                }
            }
        }

        //zz1 > 30 || zz2 > 30 ||
        if(zy1 > 30 || zy2 > 30 ||  zyyt1 > 30 || zyyt2 > 30){
            restart();
            Message message = new Message();
            message.what = -1 ;
            handler.sendMessage(message);
            return;
        }
        if(eyesActions.size() > 4){
            if(checkHas(eyesActions)){
                Message message = new Message();
                message.what = Const.ACTION_TYPE_EYE ;
                handler.sendMessage(message);
                restart();
            }
        }
//        if(mouthActions.size() > 4){
//            if(checkHas(mouthActions)){
//                Message message = new Message();
//                message.what = Const.ACTION_TYPE_MOUTH ;
//                handler.sendMessage(message);
//                restart();
//            }
//        }

        if(headActions.size() > 4){
            if(checkHas(headActions)){
                Message message = new Message();
                message.what = Const.ACTION_TYPE_LEFT_RIGHT_HEAD ;
                handler.sendMessage(message);
                restart();
            }
        }
        //超时
        if(startTime != 0 && (System.currentTimeMillis() - startTime == checkTime)) {
            restart();
        }
    }

    //动作系数
    private float coefficient = 0.33f;
    private boolean checkHas(List<Integer> list){
        int count = 0;
        for(Integer i : list){
            if(i == 1){
                count++;
            }
        }
        if(count > 0){
            return true;
        }
        return false;
    }
    //重新开始
    private void restart(){
        zy1 = 0;
        zy2= 0 ;
//        zz1 = 0;
//        zz2 = 0 ;
        zyyt1 = 0;
        zyyt2 = 0;
        eyesActions = new ArrayList<>();
//        mouthActions = new ArrayList<>();
        headActions = new ArrayList<>();
        startTime = 0;
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int type = msg.what;
            if(onFaceCheckResult != null){
                onFaceCheckResult.onAction(type);
            }
        }
    };


    private OnFaceCheckResult onFaceCheckResult;

    public OnFaceCheckResult getOnFaceCheckResult() {
        return onFaceCheckResult;
    }

    public void setOnFaceCheckResult(OnFaceCheckResult onFaceCheckResult) {
        this.onFaceCheckResult = onFaceCheckResult;
    }

    //回调 检查是否有执行该动作
    public interface OnFaceCheckResult{
        //是否执行 执行类型
        void onAction(int type);
    }
}
